package com.wyzz.hr.action;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IAttendmonthlyBO;
import com.wyzz.hr.bo.IEmpBenefitBo;
import com.wyzz.hr.bo.IEmpBenefitPlanBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmpsalaryperiodBo;
import com.wyzz.hr.bo.IPerfplanBO;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.AddEbpBean;
import com.wyzz.hr.pojo.Empbenefit;
import com.wyzz.hr.pojo.Empbenefitplan;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.pojo.Empsalaryperiod;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.StringUtil;

public class SearchEbpAction extends CompAction {

	public SearchEbpAction() {
		emp = new Employee();
		page = new Pager();
		locationList = null;
		yearMonth = DateUtil.formatTodayToS("yyyy-MM");
	}

	public String execute() throws Exception {
		getDrillDownList();
		ebpList = searchEbp();
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		ebpBo.calcEbp(ebpList);
		if (!StringUtils.isEmpty(actionErrorS)) {
			addDwrInfo(actionErrorS);
			actionErrorS = null;
		}
		setCanEdit(getEspStatus(yearMonth));
		return "success";
	}

	private List searchEbp() {
		DetachedCriteria dc = searchBenefitPlan_DC();
		addCriteria(dc);
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		return ebpBo.findEbp(dc, page, null);
	}

	private DetachedCriteria searchBenefitPlan_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(Empbenefitplan.class);
		dc.createAlias(Empbenefitplan.PROP_EBP_EMP_NO, "emp", 1);
		dc.createAlias("emp.empBenefitType", "beneType", 1);
		dc.createAlias("emp.empDeptNo", "dept", 1);
		dc.createAlias("emp.empLocationNo", "location", 1);
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 2);
		addOrders(dc, page,
				new String[] {
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString(),
						(new StringBuilder()).append(
								Empbenefitplan.PROP_EBP_BELONG_YEARMONTH)
								.append("-down").toString() });
		BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_STATUS, "eq",
				new Integer[] { Integer.valueOf(1) });
		BaseCrit.addEmpDC(dc, "emp", emp.getEmpName());
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				Integer.valueOf(1), emp.getEmpDeptNo());
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { emp.getEmpLocationNo() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_BENEFIT_TYPE).toString(), "id",
				new Object[] { emp.getEmpBenefitType() });
		BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_YEARMONTH, "eq",
				new String[] { yearMonth.replaceAll("-", "") });
	}

	private void getDrillDownList() {
		locationList = getDrillDown("Location", new String[0]);
		ebfTypeList = getDrillDown("BenefitType", new String[0]);
		empStatus = getDrillDown("EmpStatus", new String[0]);
	}

	public String saveBeneAddData(AddEbpBean ebpBean) {
		String flt = DWRUtil.checkAuth("saveBeneAddData", "saveBeneAddData");
		if ("error".equalsIgnoreCase(flt))
			return StringUtil.messageNoErrCode(msgNoAuth,
					new Object[] { "noauth" });
		if (StringUtils.isEmpty(ebpBean.getEmpId()))
			return StringUtil.message(msgSystemEx, new Object[0]); //系统异常，请刷新后重试
		// ！
		Map newBenefitMap;
		try {
			newBenefitMap = getBaseParams(ebpBean);

			System.out.println(newBenefitMap.size());

			if (StringUtils.isNotEmpty(actionErrorS))
				return actionErrorS;
			if (newBenefitMap == null || newBenefitMap.size() == 0)
				return StringUtil.messageNoErrCode(msgNoData, new Object[] {
						"nodata", "\u793E\u4FDD\u914D\u7F6E" });
			String espPeriodArr[] = checkBenefitCurrMonth(newBenefitMap,
					ebpBean.getYearMonth());

			System.out.println("dd"+espPeriodArr.length);

			if (StringUtils.isNotEmpty(actionErrorS))
				return actionErrorS;
			String ebpPeriodArr[] = checkBenefitDuplicate(newBenefitMap,
					ebpBean.getYearMonth(), ebpBean.getEmpId().split(","));

			System.out.println("ff"+ebpPeriodArr.length);

			if (StringUtils.isNotEmpty(actionErrorS))
				return actionErrorS;
			List payList = updatePayBatch_DC(espPeriodArr, ebpBean.getEmpId()
					.split(","));
			String[] s = ebpBean.getEmpId().split(",");
			for (int i = 0; i < s.length; i++)
				System.out.println(s[i]);
			for (int i = 0; i < espPeriodArr.length; i++)
				System.out.println(espPeriodArr[i]);
			payList = mergePayBenefitConfig(payList, newBenefitMap);
			System.out.println(payList.size());
			if (payList == null || payList.size() == 0)
				return StringUtil.message(msgSystemEx, new Object[0]);
			IEmpSalaryAcctitemsBo esaiBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
					.getBean("empsalaryacctitemsBo");
			if (!esaiBo.setAcctItemsPay((Empsalarypay[]) payList
					.toArray(new Empsalarypay[payList.size()])))
				return StringUtil.messageNoErrCode(msgNoData, new Object[] {
						"nodata", "\u5E10\u5957\u6570\u636E" });//帐套数据{0}:没有{1}数据
			// ，请刷新后重试！
			IAttendmonthlyBO attendmonBo = (IAttendmonthlyBO) SpringBeanFactory
					.getBean("attendmonthlyBo");
			for (int i = 0; i < espPeriodArr.length; i++)
				attendmonBo.setAttmByPay(espPeriodArr[i],
						(Empsalarypay[]) payList
								.toArray(new Empsalarypay[payList.size()]));

			IPerfplanBO perfplanBO = (IPerfplanBO) SpringBeanFactory
					.getBean("perfplanBO");
			for (int i = 0; i < espPeriodArr.length; i++)
				perfplanBO.setPerfplanByPay(espPeriodArr[i],
						(Empsalarypay[]) payList
								.toArray(new Empsalarypay[payList.size()]));

			for (int i = 0; i < espPeriodArr.length; i++)
				interpretPay(espPeriodArr[i], true, (Empsalarypay[]) payList
						.toArray(new Empsalarypay[payList.size()]));

			IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
			Map ebpMap = ebpBo.findEbpBelongYMs(ebpPeriodArr, null, true,
					ebpBean.getEmpId().split(","));
			List planList = geneBenefitPlans(payList, ebpMap, ebpBean
					.getYearMonth());
			if (planList == null || planList.size() == 0) {
				return StringUtil
						.messageNoErrCode(
								msgNoData,
								new Object[] { "nobenefit",
										"\u914D\u7F6E\u5E10\u5957\u4E2D\u793E\u4FDD\u9879\u76EE" });// 配置帐套中社保项目
			} else {
				String info = ebpBo.batchSaveEbp(planList);
				return StringUtil.message(msgAddBeneSucc, new Object[] {
						"SUCC", info, ebpBean.getYearMonth() });
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private Map getBaseParams(AddEbpBean ebpBean)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		String empIds[];
		List empList;
		Map newBenefitMap;
		int i;
		empIds = ebpBean.getEmpId().split(",");
		ISalaryconfBo configBo = (ISalaryconfBo) SpringBeanFactory
				.getBean("salaryconfBo");
		empList = configBo.getEmployeeWithConfig(empIds);
		if (empList == null || empList.size() == 0) {
			actionErrorS = StringUtil.message(msgSystemEx, new Object[0]);
			return null;
		}
		for (Iterator i$ = empList.iterator(); i$.hasNext();) {
			Employee employee = (Employee) i$.next();
			if (employee.getEmpBenefitType() == null) {
				actionErrorS = StringUtil.messageNoErrCode(msgNoBenefit,
						new Object[] { "nobenefit", employee.getEmpName() });
				return null;
			}
			if (empIds.length > 1 && employee.getBenefit() == null) {
				actionErrorS = StringUtil.messageNoErrCode(msgNoBenefit,
						new Object[] { "nobenefit", employee.getEmpName() });
				return null;
			}
		}

		newBenefitMap = new HashMap();
		i = 1;
		while (i < 7) {
			Empbenefit benefit;
			boolean hasBaseValue;
			benefit = new Empbenefit();
			hasBaseValue = false;
			Object yearmonth;
			Object pension;
			Object housing;
			Object insurance;
			Object memo;
			try {
				yearmonth = PropertyUtils.getProperty(ebpBean,
						(new StringBuilder()).append("addyearmonth").append(i)
								.toString());
				if (StringUtils.isEmpty((String) yearmonth)) {
					i++;
					continue;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			benefit.setEbfStartMonth((String) yearmonth);
			benefit.setEbfEndMonth((String) yearmonth);
			benefit.setBelongYearMonth((String) yearmonth);
			pension = PropertyUtils.getProperty(ebpBean, (new StringBuilder())
					.append("ebpPensionAmount").append(i).toString());
			if (StringUtils.isEmpty((String) pension)) {
				benefit.setEbfPensionAmount(new BigDecimal("0.00"));
			} else {
				benefit.setEbfPensionAmount((new BigDecimal((String) pension))
						.setScale(2));
				hasBaseValue = true;
			}
			housing = PropertyUtils.getProperty(ebpBean, (new StringBuilder())
					.append("ebpHousingAmount").append(i).toString());
			if (StringUtils.isEmpty((String) housing)) {
				benefit.setEbfHousingAmount(new BigDecimal("0.00"));
			} else {
				benefit.setEbfHousingAmount((new BigDecimal((String) housing))
						.setScale(2));
				hasBaseValue = true;
			}
			insurance = PropertyUtils.getProperty(ebpBean,
					(new StringBuilder()).append("ebpInsuranceAmount")
							.append(i).toString());
			if (StringUtils.isEmpty((String) insurance)) {
				benefit.setEbfInsuranceAmount(new BigDecimal("0.00"));
			} else {
				benefit.setEbfInsuranceAmount((new BigDecimal(
						(String) insurance)).setScale(2));
				hasBaseValue = true;
			}
			memo = PropertyUtils.getProperty(ebpBean, (new StringBuilder())
					.append("memo").append(i).toString());
			if (StringUtils.isNotEmpty((String) memo))
				benefit.setEbfComments((String) memo);
			if (hasBaseValue) {
				for (Iterator i$ = empList.iterator(); i$.hasNext();) {
					Employee employee = (Employee) i$.next();
					try {
						Empbenefit benefitEmp = (Empbenefit) benefit.clone();
						employee.setBenefit(benefitEmp);
						benefitEmp.setEmployee(employee);
					} catch (CloneNotSupportedException e) {
						e.printStackTrace();
					}
				}

			} else {
				if (empIds.length == 1)
					continue;
				if (empIds.length > 1) {
					IEmpBenefitBo empbenefitBo = (IEmpBenefitBo) SpringBeanFactory
							.getBean("empbenefitBo");
					empbenefitBo.getEmpBenefitEbp(benefit.getEbfStartMonth(),
							empList);
				}
			}
			Employee employee;
			Empbenefit benefitMapItem;
			for (Iterator i$ = empList.iterator(); i$.hasNext(); newBenefitMap
					.put((new StringBuilder()).append(
							benefitMapItem.getBelongYearMonth()).append(" ")
							.append(employee.getEmpDistinctNo()).append(" ")
							.append(employee.getEmpName()).toString(),
							benefitMapItem)) {
				employee = (Employee) i$.next();
				benefitMapItem = employee.getBenefit();
				if (employee.getEmpBenefitType().getBenefitTypePayType()
						.equals(Integer.valueOf(0)))
					benefitMapItem.setEspYearMonth(benefit.getEbfStartMonth());
				else
					benefitMapItem.setEspYearMonth(DateUtil.yearMonthAdd(
							benefit.getEbfStartMonth(), -1, 2));
				String memo1 = benefit.getEbfComments();
				if (StringUtils.isNotEmpty(memo1))
					benefitMapItem.setEbfComments(memo1);
			}
			i++;
		}
		return newBenefitMap;
	}

	private String[] checkBenefitCurrMonth(Map newBenefitMap, String currYM) {
		currYM = currYM.replaceAll("-", "");
		Set periodSet = new HashSet();
		String yearMonth;
		for (Iterator i$ = newBenefitMap.entrySet().iterator(); i$.hasNext(); periodSet
				.add(yearMonth)) {
			java.util.Map.Entry benefitEntry = (java.util.Map.Entry) i$.next();
			Empbenefit benefit = (Empbenefit) benefitEntry.getValue();
			yearMonth = benefit.getEspYearMonth();
			if (yearMonth.equals(currYM)) {
				actionErrorS = StringUtil.messageNoErrCode(msgDupBenefitCur,
						new Object[] { "currym", yearMonth });
				return null;
			}
		}

		String periodArr[] = (String[]) periodSet.toArray(new String[periodSet
				.size()]);
		String allPeriodArr[] = StringUtil.merge1D(periodArr, currYM);
		IEmpsalaryperiodBo periodBo = (IEmpsalaryperiodBo) SpringBeanFactory
				.getBean("empsalaryperiodBO");
		List periodList = periodBo.getPayPeriods(null, allPeriodArr);
		for (int i = 0; periodList != null && i < periodList.size(); i++) {
			Empsalaryperiod period = (Empsalaryperiod) periodList.get(i);
			Integer status = period.getEspdStatus();
			String periodYm = period.getEspdYearmonth();
			if (periodYm.equals(currYM) && !status.equals(Integer.valueOf(0))) {
				actionErrorS = StringUtil.messageNoErrCode(msgStatErrBeneC,
						new Object[] { "staterr", periodYm });
				return null;
			}
			if (!periodYm.equals(currYM) && !status.equals(Integer.valueOf(2))) {
				actionErrorS = StringUtil.messageNoErrCode(msgStatErrBeneP,
						new Object[] { "staterr", periodYm });
				return null;
			}
		}

		return periodArr;
	}

	private String[] checkBenefitDuplicate(Map newBenefitMap, String currYM,
			String empIds[]) {
		currYM = currYM.replaceAll("-", "");
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		Map ebpMap = ebpBo.findEbpYMs(new String[] { currYM }, new Integer(1),
				false, empIds);
		if (ebpMap == null)
			ebpMap = new HashMap();
		Set periodSet = new HashSet();
		for (Iterator i$ = newBenefitMap.entrySet().iterator(); i$.hasNext();) {
			java.util.Map.Entry benefitEntry = (java.util.Map.Entry) i$.next();
			Empbenefit benefit = (Empbenefit) benefitEntry.getValue();
			String beneKey = (String) benefitEntry.getKey();
			String yearMonth = benefit.getBelongYearMonth();
			periodSet.add(yearMonth);
			if (ebpMap.get(beneKey) != null) {
				actionErrorS = StringUtil.messageNoErrCode(msgDupBenefitPre,
						new Object[] { "dupym", beneKey });
				return null;
			}
		}

		return (String[]) periodSet.toArray(new String[periodSet.size()]);
	}

	private List updatePayBatch_DC(String yearmonth[], String empIds[]) {
		DetachedCriteria dc = CompAction.updatePayBatch_DC();
		BaseCrit.addDC(dc, Empsalarypay.PROP_ESP_YEARMONTH, "in", yearmonth);
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_ID).toString(), "in", empIds);
		BaseAction.addOrders(dc, null,
				new String[] {
						(new StringBuilder()).append(
								Empsalarypay.PROP_ESP_YEARMONTH).append("-up")
								.toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) SpringBeanFactory
				.getBean("salaryPaidBo");
		List payList = salaryPaidBo.findPay(dc, null);
		if (payList == null || payList.size() == 0)
			return null;
		Empsalaryconfig config;
		for (Iterator i$ = payList.iterator(); i$.hasNext(); config
				.decryEmpSalaryConf(config)) {
			Empsalarypay pay = (Empsalarypay) i$.next();
			config = pay.getEspEmpconfig();
			pay.decryEMPPaid(pay);
		}

		return payList;
	}

	private List mergePayBenefitConfig(List payList, Map newBenefitMap) {
		if (payList == null || payList.size() == 0) {
			payList = new ArrayList();
		} else {
			for (Iterator it = payList.iterator(); it.hasNext();) {
				Empsalarypay pay = (Empsalarypay) it.next();
				String yearmonth = DateUtil.yearMonthAdd(pay.getEspYearmonth(),
						pay.getEspEmpno().getEmpBenefitType()
								.getBenefitTypePayType().intValue(), 2);
				String key = (new StringBuilder()).append(yearmonth)
						.append(" ").append(
								pay.getEspEmpno().getEmpDistinctNo()).append(
								" ").append(pay.getEspEmpno().getEmpName())
						.toString();
				Empbenefit benefit = (Empbenefit) newBenefitMap.get(key);
				if (benefit != null) {
					pay.setBenefit(benefit);
					pay.setBenefitType(pay.getEspEmpno().getEmpBenefitType());
					newBenefitMap.remove(key);
				} else {
					it.remove();
				}
			}

		}
		Iterator i$ = newBenefitMap.keySet().iterator();
		do {
			if (!i$.hasNext())
				break;
			String key = (String) i$.next();
			Empbenefit benefit = (Empbenefit) newBenefitMap.get(key);
			Empsalaryconfig config = benefit.getEmployee().getConfig();
			if (config != null) {
				config.decryEmpSalaryConf(config);
				Empsalarypay pay = copyConfigToPay(config);
				pay.setEspYearmonth(benefit.getEspYearMonth());
				pay.setBenefit(benefit);
				pay.setBenefitType(pay.getEspEmpno().getEmpBenefitType());
				payList.add(pay);
			}
		} while (true);
		if (payList != null && payList.size() > 0) {
			Empsalarypay pay;
			for (i$ = payList.iterator(); i$.hasNext(); pay
					.setEspBenefitPlans(Integer.valueOf(1)))
				pay = (Empsalarypay) i$.next();

		}
		return payList;
	}

	private List geneBenefitPlans(List payList, Map ebpMap, String currYM) {
		currYM = currYM.replaceAll("-", "");
		List planList = new ArrayList();
		String lastChangeBy = getCurrentEmp().getId();
		Date lastChangeTime = new Date();
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		Iterator i$ = payList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Empsalarypay pay = (Empsalarypay) i$.next();
			String comments = pay.getBenefit().getEbfComments();
			String yearMonth = pay.getBenefit().getBelongYearMonth();
			String key = (new StringBuilder()).append(yearMonth).append(" ")
					.append(pay.getEspEmpno().getEmpDistinctNo()).append(" ")
					.append(pay.getEspEmpno().getEmpName()).toString();
			Empbenefitplan planPaid = null;
			Empbenefitplan planRemain = null;
			Empbenefitplan planAll = pay.getBenefitPlan();
			if (ebpMap != null) {
				planPaid = (Empbenefitplan) ebpMap.get(key);
				planRemain = ebpBo.deductEbpYMs(planAll, planPaid);
			} else {
				planRemain = planAll;
			}
			if (planRemain != null) {
				planRemain.setEbpEsavId(pay.getEspEsavId());
				planRemain.setEbpEmpno(pay.getEspEmpno());
				planRemain.setEbpYearMonth(currYM);
				planRemain.setEbpBelongYearmonth(yearMonth);
				planRemain.setEbpStatus(new Integer(1));
				planRemain.setEbpComments(comments);
				planRemain.setEbpCreateBy(lastChangeBy);
				planRemain.setEbpCreateTime(lastChangeTime);
				planRemain.setEbpLastChangeBy(lastChangeBy);
				planRemain.setEbpLastChangeTime(lastChangeTime);
				planRemain.encryEMPPlan(planRemain);
				planList.add(planRemain);
			}
		} while (true);
		return planList;
	}

	public String deleteEbpById() {
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		if (StringUtils.isEmpty(ebpId)) {
			addErrorInfo("\u8BF7\u81F3\u5C11\u9009\u62E9\u4E00\u6761\u8BB0\u5F55\uFF01");
			return "success";
		}
		List ebpList = ebpBo.searchEbpById(ebpId.split(","));
		if (ebpList == null) {
			addErrorInfo("\u5220\u9664\u5931\u8D25\uFF01\u8BF7\u5237\u65B0\u9875\u9762\u540E\u91CD\u8BD5\u3002");
			return "success";
		}
		ebpBo.deleteEbpById(ebpId.split(","));
		String yearMontht = null;
		String info = "";
		try {
			for (Iterator i$ = ebpList.iterator(); i$.hasNext();) {
				Empbenefitplan plan = (Empbenefitplan) i$.next();
				yearMontht = DateUtil.formatDateToS(DateUtil.parseDateByFormat(
						plan.getEbpBelongYearmonth(), "yyyyMM"),
						"yyyy\u5E74MM\u6708");
				info = StringUtil.addConcatStr(info, "\u3001",
						new Object[] { (new StringBuilder()).append(
								plan.getEbpEmpno().getEmpName()).append(
								yearMontht).toString() });
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		addSuccessInfo((new StringBuilder())
				.append("\u60A8\u5DF2\u5220\u9664")
				.append(info)
				.append(
						"\u793E\u4FDD\u8865\u7F34\u8BB0\u5F55\uFF0C\u6539\u52A8\u5C06\u5728\u91CD\u65B0\u521D\u59CB\u5316\u85AA\u8D44\u540E\u751F\u6548\u3002")
				.toString());
		return "success";
	}

	public List getEbpItemsById(String ebpId) {
		String flt = DWRUtil.checkAuth("getEbpById", "getEbpById");
		if ("error".equalsIgnoreCase(flt))
			return null;
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		List ebpList = ebpBo.searchEbpById(new String[] { ebpId });
		if (ebpList == null)
			return null;
		Empbenefitplan ebp = (Empbenefitplan) ebpList.get(0);
		ebp.decryEMPPlan(ebp);
		IEmpSalaryAcctitemsBo itemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List acctItems = itemsBo.getItemsByAcctversion(ebp.getEbpEsavId()
				.getId());
		for (Iterator it = acctItems.iterator(); it.hasNext();) {
			Empsalaryacctitems item = (Empsalaryacctitems) it.next();
			try {
				Object value = PropertyUtils.getProperty(ebp,
						(new StringBuilder()).append("ebpColumn").append(
								item.getEsaiDataSeq()).toString());
				if (value != null)
					item.setItemValue((BigDecimal) value);
				else
					item.setItemValue(new BigDecimal(0.0D));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return acctItems;
	}

	public Map getEbpByEmpMonth(String empId, String yearMonth) {
		Map result = new HashMap();
		String flt = DWRUtil.checkAuth("getEbpByEmpMonth", "getEbpByEmpMonth");
		if ("error".equalsIgnoreCase(flt)) {
			result.put("error", StringUtil.messageNoErrCode(msgNoAuth,
					new Object[] { "noauth" }));
			return result;
		}
		if (StringUtils.isEmpty(empId)
				|| DateUtil.parseDateNoEx(yearMonth, "yyyyMM") == null) {
			result.put("error", StringUtil.messageNoErrCode(msgNoParam,
					new Object[] { "noparam" }));
			return result;
		} else {
			IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
			Empbenefitplan ebp = ebpBo.searchEmpbenefitByEmpMonth(empId,
					yearMonth);
			result.put("param_succ", ebp);
			return result;
		}
	}

	public List getEbpByEmpMonths(String empId, String yearMonthArr[]) {
		String flt = DWRUtil.checkAuth("getEbpByEmpMonth", "getEbpByEmpMonth");
		if ("error".equalsIgnoreCase(flt)) {
			return null;
		} else {
			IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
			List ebpList = ebpBo.searchEbpByEmpMonths(empId, yearMonthArr);
			return ebpList;
		}
	}

	public Empbenefitplan modifyEbp(String ebpId, String columns,
			String modiComments) {
		String flt = DWRUtil.checkAuth("modifyEbp", "modifyEbp");
		if ("error".equalsIgnoreCase(flt))
			return null;
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		List ebpList = ebpBo.searchEbpById(new String[] { ebpId });
		if (ebpList == null || ebpList.size() == 0)
			return null;
		Empbenefitplan ebp = (Empbenefitplan) ebpList.get(0);
		String values[] = columns.split(",");
		List valueList = new ArrayList();
		for (int i = 0; i < values.length; i++) {
			DecimalFormat df = new DecimalFormat("#.00");
			valueList.add(new BigDecimal(df.format(Double
					.parseDouble(values[i]))));
		}

		IEmpSalaryAcctitemsBo itemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List acctItems = itemsBo.getItemsByAcctversion(ebp.getEbpEsavId()
				.getId());
		Class ownerClass = ebp.getClass();
		Method ebpMethod = null;
		int index = 0;
		for (Iterator i$ = acctItems.iterator(); i$.hasNext();) {
			Empsalaryacctitems item = (Empsalaryacctitems) i$.next();
			try {
				if (item.getEsaiEsdd().getEsddDataType().intValue() >= 10
						|| item.getEsaiEsdd().getEsddDataType().intValue() <= 16) {
					ebpMethod = ownerClass.getMethod((new StringBuilder())
							.append("setEbpColumn").append(
									item.getEsaiDataSeq()).toString(),
							new Class[] { BigDecimal.class });
					ebpMethod
							.invoke(ebp, new Object[] { valueList.get(index) });
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			index++;
		}

		ebp.setEbpComments(modiComments);
		ebp.setEbpLastChangeBy(getCurrentEmp().getId());
		ebp.setEbpLastChangeTime(new Date());
		Empbenefitplan returnEbp = null;
		try {
			returnEbp = (Empbenefitplan) ebp.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		ebp.encryEMPPlan(ebp);
		ebpBo.updateEbp(ebp);
		returnEbp = ebpBo.calcEbpForPage(returnEbp, acctItems);
		return returnEbp;
	}

	public String getEspStatus(String yearMonth) {
		yearMonth = yearMonth.replaceAll("-", "");
		IEmpsalaryperiodBo periodBo = (IEmpsalaryperiodBo) getBean("empsalaryperiodBO");
		Empsalaryperiod period = periodBo.loadEspdStatus(yearMonth);
		if (period == null || period.getEspdStatus().intValue() == 0)
			return "true";
		else
			return "false";
	}

	public List getLocationList() {
		return locationList;
	}

	public void setLocationList(List locationList) {
		this.locationList = locationList;
	}

	public List getEbfTypeList() {
		return ebfTypeList;
	}

	public void setEbfTypeList(List ebfTypeList) {
		this.ebfTypeList = ebfTypeList;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public String getEmpCondition() {
		return empCondition;
	}

	public void setEmpCondition(String empCondition) {
		this.empCondition = empCondition;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getYearMonth() {
		return yearMonth;
	}

	public void setYearMonth(String yearMonth) {
		this.yearMonth = yearMonth;
	}

	public List getEbpList() {
		return ebpList;
	}

	public void setEbpList(List ebpList) {
		this.ebpList = ebpList;
	}

	public String getEbpId() {
		return ebpId;
	}

	public void setEbpId(String ebpId) {
		this.ebpId = ebpId;
	}

	public String getCanEdit() {
		return canEdit;
	}

	public void setCanEdit(String canEdit) {
		this.canEdit = canEdit;
	}

	public String getActionErrorS() {
		return actionErrorS;
	}

	public void setActionErrorS(String actionErrorS) {
		this.actionErrorS = actionErrorS;
	}

	private static final long serialVersionUID = 1L;
	private String empCondition;
	private Employee emp;
	private Pager page;
	private List locationList;
	private List ebfTypeList;
	private String yearMonth;
	private List ebpList;
	private String ebpId;
	private String canEdit;
	private String actionErrorS;
}
